package com.softaria.promosite.client;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.softaria.windows.config.MenuConfiguration;
import com.softaria.windows.factories.BadUrlException;
import com.softaria.windows.factories.CloseListenerRegistrator;
import com.softaria.windows.factories.TypedViewFactory;
import com.softaria.windows.logic.HelpWriter;
import com.softaria.windows.model.View;
import com.softaria.windows.model.ViewUrl;
import com.softaria.windows.widgets.ResizeListenerRegistrator;

public class TextTypedViewFactory implements TypedViewFactory {

  private static Map<String, String> texts = new HashMap<String, String>();

  private MenuConfiguration menu;

  public TextTypedViewFactory(MenuConfiguration menu) {
    super();
    this.menu = menu;
  }

  public View createView(Map<String, List<String>> params,
      CloseListenerRegistrator closeListenerRegistrator, ResizeListenerRegistrator frlr,
      HelpWriter helpWriter) throws BadUrlException {

    View view = new View() {

      @Override
      public void setViewUrl(ViewUrl viewUrl) {
        super.setViewUrl(viewUrl);

        setName(menu.getMenuLabel(viewUrl));
      }

    };

    final String path = getParameter(params, "path");

    final Grid grid = new Grid(1, 1);

    grid.getCellFormatter().setAlignment(0, 0, HasHorizontalAlignment.ALIGN_LEFT,
        HasVerticalAlignment.ALIGN_TOP);

    grid.setStylePrimaryName("grids");

    final HTML label = new HTML();

    label.setStylePrimaryName("labels");

    grid.setWidget(0, 0, label);

    view.setContent(grid);

    // frlr.addResizeListener(new FrameResizeListener() {
    //
    // public void onResize(int height, int width) {
    // grid.setSize(width + "px", height + "px");
    // }
    //
    // });

    String cachedText = texts.get(path);

    if (cachedText != null) {
      label.setHTML(cachedText);
    } else {
      // start asynchronous loading text from server

      ContentServiceAsync contentService = ContentServiceProvider.getContentService();

      contentService.getTextContent(path, new AsyncCallback<String>() {

        public void onFailure(Throwable caught) {
          label.setText("Can't load text for path=" + path);

        }

        public void onSuccess(String result) {

          texts.put(path, result);
          label.setHTML(result);

        }

      });
    }

    return view;
  }

  private String getParameter(Map<String, List<String>> params, String key) {
    List<String> labels = params.get(key);

    if ((labels != null) && (labels.size() > 0)) {
      return labels.get(0);
    } else {
      return null;
    }
  }

}
